package com.csit.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="T_Sale")
public class Sale implements Serializable {

	private static final long serialVersionUID = 763645641934751054L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Integer saleID;
	//销售单编号
	@Column(length=20)
	private String saleCode;
	//销售日期
	@Temporal(TemporalType.DATE)
	private Date saleDate;
	//总金额
	private Double amount;
	//折扣金额
	private Double discountAmount;
	//经办人
	@Column(length=100)
	private String saleer;
	//现金
	private Double cash;
	//刷卡
	private Double card;
	//会员卡
	private Double member;
	//备注
	@Column(length=2000)
	private String note;
	//店铺
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="shopID")
	private DataDictionary shop;
	//出货仓库
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="storeHouseID")
	private DataDictionary storeHouse;
	//客户
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="customerID")
	private Customer customer;
	
	@Column(updatable=false)
	@Temporal(TemporalType.DATE)
	private Date createDate;
	
	@OneToMany(fetch=FetchType.LAZY,mappedBy="sale")
	@OrderBy("position")
	List<SaleDetail> saleDetailList = new ArrayList<SaleDetail>();

	public Integer getSaleID() {
		return saleID;
	}

	public void setSaleID(Integer saleID) {
		this.saleID = saleID;
	}

	public String getSaleCode() {
		return saleCode;
	}

	public void setSaleCode(String saleCode) {
		this.saleCode = saleCode;
	}

	public Date getSaleDate() {
		return saleDate;
	}

	public void setSaleDate(Date saleDate) {
		this.saleDate = saleDate;
	}

	public Double getAmount() {
		return amount;
	}

	public void setAmount(Double amount) {
		this.amount = amount;
	}

	public Double getDiscountAmount() {
		return discountAmount;
	}

	public void setDiscountAmount(Double discountAmount) {
		this.discountAmount = discountAmount;
	}

	public String getSaleer() {
		return saleer;
	}

	public void setSaleer(String saleer) {
		this.saleer = saleer;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public DataDictionary getShop() {
		return shop;
	}

	public void setShop(DataDictionary shop) {
		this.shop = shop;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public List<SaleDetail> getSaleDetailList() {
		return saleDetailList;
	}

	public void setSaleDetailList(List<SaleDetail> saleDetailList) {
		this.saleDetailList = saleDetailList;
	}

	public Double getCash() {
		return cash;
	}

	public void setCash(Double cash) {
		this.cash = cash;
	}

	public Double getCard() {
		return card;
	}

	public void setCard(Double card) {
		this.card = card;
	}

	public Double getMember() {
		return member;
	}

	public void setMember(Double member) {
		this.member = member;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public DataDictionary getStoreHouse() {
		return storeHouse;
	}

	public void setStoreHouse(DataDictionary storeHouse) {
		this.storeHouse = storeHouse;
	}
	
	@Transient
	public void setShopID(Integer shopID){
		if(shop==null){
			shop = new DataDictionary();
		}
		shop.setDataDictionaryID(shopID);
	}
	
	@Transient
	public String getCustomerName(){
		if(customer==null){
			return null;
		}
		return customer.getCustomerName();
	}
	
	@Transient
	public String getStoreHouseName(){
		if(storeHouse==null){
			return null;
		}
		return storeHouse.getDataDictionaryName();
	}
}
